<!DOCTYPE html>
<meta charset="UTF-8">
<title>SVGTextContentElement query interface RTL</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<svg width="50px" height="60px" font-family="Arial" font-size="30px">
  <text x="0" y="50">הפוך</text>
</svg>
<script>
setup(function() {
  window.textElement = document.querySelector("text");
  window.totalExtent = textElement.getComputedTextLength();
});

test(function() {
  assert_equals(textElement.textContent.length, 4);
  var positions = [];
  for (var i = 0; i < 4; ++i)
    positions.push(textElement.getStartPositionOfChar(i).x);

  assert_approx_equals(positions[0], totalExtent, 0.5);
  assert_less_than(positions[1], positions[0]);
  assert_less_than(positions[2], positions[1]);
  assert_less_than(positions[3], positions[2]);
  assert_greater_than(positions[3], 0);
}, document.title+', getStartPositionOfChar().');

test(function() {
  assert_equals(textElement.textContent.length, 4);
  var positions = [];
  for (var i = 0; i < 4; ++i)
    positions.push(textElement.getEndPositionOfChar(i).x);

  assert_less_than(positions[0], totalExtent);
  assert_less_than(positions[1], positions[0]);
  assert_less_than(positions[2], positions[1]);
  assert_less_than(positions[3], positions[2]);
  assert_approx_equals(positions[3], 0, 0.5);
}, document.title+', getEndPositionOfChar().');

test(function() {
  assert_equals(textElement.textContent.length, 4);
  var bounds = [];
  for (var i = 0; i < 4; ++i)
    bounds.push(textElement.getExtentOfChar(i));

  function right(r) {
    return r.x + r.width;
  }

  // Verify right sides descending.
  assert_approx_equals(right(bounds[0]), totalExtent, 0.5);
  assert_less_than(right(bounds[1]), right(bounds[0]));
  assert_less_than(right(bounds[2]), right(bounds[1]));
  assert_less_than(right(bounds[3]), right(bounds[2]));
  assert_greater_than(right(bounds[3]), 0);

  // Verify left sides descending.
  assert_less_than(bounds[0].x, totalExtent);
  assert_less_than(bounds[1].x, bounds[0].x);
  assert_less_than(bounds[2].x, bounds[1].x);
  assert_less_than(bounds[3].x, bounds[2].x);
  assert_approx_equals(bounds[3].x, 0, 0.5);

  // Verify approx. adjacent.
  assert_approx_equals(bounds[0].x, right(bounds[1]), 0.5);
  assert_approx_equals(bounds[1].x, right(bounds[2]), 0.5);
  assert_approx_equals(bounds[2].x, right(bounds[3]), 0.5);
}, document.title+', getExtentOfChar().');

test(function() {
  assert_equals(textElement.textContent.length, 4);

  var prevOffset = 3;
  var queryPoint = document.querySelector('svg').createSVGPoint();
  queryPoint.y = 40;

  // Sample left-to-right, expecting a decreasing character number.
  for (var queryX = 0.5; queryX < totalExtent - 0.5; queryX += 0.5) {
    queryPoint.x = queryX;
    var offset = textElement.getCharNumAtPosition(queryPoint);
    assert_less_than_equal(offset, prevOffset);
    prevOffset = offset;
  }
  assert_equals(prevOffset, 0);

  // Sample right-to-left, expecting an increasing character number.
  for (var queryX = totalExtent - 0.5; queryX >= 0.5; queryX -= 0.5) {
    queryPoint.x = queryX;
    var offset = textElement.getCharNumAtPosition(queryPoint);
    assert_greater_than_equal(offset, prevOffset);
    prevOffset = offset;
  }
  assert_equals(prevOffset, 3);
}, document.title+', getCharNumAtPosition().');
</script>
